<?php
/**
* This file is part of the Agora distribution. 
* Detailed copyright and licensing information can be found
* in the gpl-3.0.txt file which should be included in the distribution.
* 
* @version		$Id: 3.0.14 Olympus 2010-09-28 rammstein4o $
* @copyright  2007 - 2010 jVitals
* @license   GPLv3 Open Source
* @link       http://jvitals.com
* @since      File available since initial release
*/
defined ('IN_AGORA') or die;

class ModerationModel extends Model {
	var $_level;
	var $allowed_forums;
	var $allowed_forums_arr;
	var $is_superadmin;

	function __construct() {
		parent::__construct('posts');
		$this->allowed_forums = '0';
		$this->allowed_forums_arr = array();
		$this->is_superadmin = false;
	}
	
	function _setAllowedForumsArr($arr) {
		$this->allowed_forums_arr = $arr;
	}

	function _setAllowedForums() {
		if (is_array($this->allowed_forums_arr) && count($this->allowed_forums_arr)) {
			$this->allowed_forums = implode(', ', $this->allowed_forums_arr);
		}
	}
	
	function _setSuperAdmin($value) {
		$this->is_superadmin = $value;
	}

	// $type : 0 - unapproved topics, 1- unapproved posts
	
	function countUnapproved($type = 1) {
		$sql = 'SELECT COUNT(*) ' .
				'FROM #__agora_posts AS p ' .
				'JOIN #__agora_topics AS t ' .
					'ON p.topic_id = t.id ' .
				'JOIN #__agora_forums AS f ' .
					'ON t.forum_id = f.id ' .
				'WHERE p.approved = 0 and t.approved = ' . (int)$type .
					(!$this->is_superadmin ? ' AND f.id IN(' . $this->allowed_forums . ') ' : '') .
				' ORDER BY p.posted DESC';

		$this->db->setQuery($sql);
		return $this->db->loadResult();
	}

	function getUnapproved($per_page, $page, $type = 1) {
		$limit = $this->getLimit($per_page, $page);

		$sql = 'SELECT p.id, p.posted, p.message, p.poster, p.poster_id, p.poster_ip, p.hide_smilies,  ' . 
						't.subject, t.descrip_t, t.id as topic_id, f.forum_name, f.id as forum_id, au.jos_id ' .
				'FROM #__agora_posts AS p ' .
				'JOIN #__agora_topics AS t ' .
					'ON p.topic_id = t.id ' .
				'JOIN #__agora_forums AS f ' .
					'ON t.forum_id = f.id ' .
				'LEFT JOIN #__agora_users AS au ' .
					'ON p.poster_id = au.id ' .
				'WHERE p.approved = 0 and t.approved = ' . (int)$type .
					(!$this->is_superadmin ? ' AND f.id IN(' . $this->allowed_forums . ') ' : '') .
				' ORDER BY p.posted DESC';
		$sql .= $limit;

		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}

	function approvePost($post_id, $topic_id, $forum_id, $user_id, $username) {
		
		$this->db->setQuery('UPDATE #__agora_posts SET approved = 1 WHERE id = ' . $this->db->Quote($post_id));
		$this->db->query();

		$this->db->setQuery('UPDATE #__agora_topics SET ' .
								'last_post = ' . $this->db->Quote(AGORA_TIME) . ', ' .
								'last_post_id = ' . $this->db->Quote($post_id) . ', ' .
								'last_poster = ' . $this->db->Quote($username) . ', ' .
								'num_replies = num_replies+1 ' .
							'WHERE id = ' . $this->db->Quote($topic_id));
		$this->db->query();


		$this->db->setQuery('UPDATE #__agora_forums SET ' .
								'last_post = ' . $this->db->Quote(AGORA_TIME) . ', ' .
								'last_post_id = ' . $this->db->Quote($post_id) . ', ' .
								'last_poster = ' . $this->db->Quote($username) . ', ' .
								'num_posts = num_posts+1 ' .
							'WHERE id = ' . $this->db->Quote($forum_id));
		$this->db->query();

		$this->db->setQuery('UPDATE #__agora_users SET ' .
								'num_posts = num_posts+1, ' .
								'last_post = ' . $this->db->Quote(AGORA_TIME) . 
							' WHERE id = ' . $this->db->Quote($user_id));
		$this->db->query();
		
	}

	function approveTopic($forum_id, $topic_id, $post_id, $user_id, $username) {
        
		$this->db->setQuery('UPDATE #__agora_topics SET approved = 1 WHERE id = ' . $this->db->Quote($topic_id));
		$this->db->query();

		$this->db->setQuery('UPDATE #__agora_posts SET approved = 1 WHERE topic_id = ' . $this->db->Quote($topic_id));
		$this->db->query();

		$this->db->setQuery('UPDATE #__agora_forums SET ' .
								'last_post = ' . $this->db->Quote(AGORA_TIME) . ', ' .
								'last_post_id = ' . $this->db->Quote($post_id) . ', ' .
								'last_poster = ' . $this->db->Quote($username) . ', ' .
								'num_topics = num_topics+1 ' .
							'WHERE id=' . $this->db->Quote($forum_id));
		$this->db->query();

		$this->db->setQuery('UPDATE #__agora_users SET ' .
								'num_posts = num_posts+1, ' .
								'last_post = ' . $this->db->Quote(AGORA_TIME) .
							' WHERE id = ' . $this->db->Quote($user_id));
		$this->db->query();

	}
	
	function declinePost($id) {
		$this->db->setQuery('DELETE FROM #__agora_posts WHERE id = ' . $this->db->Quote($id));
		$this->db->query();
	}
	
	function declineTopic($id) {
		$this->db->setQuery('DELETE FROM #__agora_topics WHERE id = ' . $this->db->Quote($id));
		$this->db->query();
        $this->db->setQuery('DELETE FROM #__agora_posts WHERE topic_id = ' . $this->db->Quote($id));
        $this->db->query();
        $this->db->setQuery('DELETE FROM #__agora_subscriptions WHERE topic_id = ' . $this->db->Quote($id));
        $this->db->query();
	}

	function declineAllPosts() {
		$sql = 'SELECT p.id FROM #__agora_posts p JOIN #__agora_topics t ON p.topic_id = t.id ' .
				'WHERE p.approved = 0 and t.approved = 1 ' . (!$this->is_superadmin ? ' AND t.forum_id IN(' . $this->allowed_forums . ') ' : '');
		$this->db->setQuery($sql);
		$posts = implode(', ', $this->db->loadResultArray());
		$posts = $posts ? $posts : 0;
		
		$this->db->setQuery('DELETE FROM #__agora_posts WHERE id IN (' . $posts . ')');
		$this->db->query();
	}
	
	function declineAllTopics() {
		$sql = 'SELECT id FROM #__agora_topics WHERE approved = 0 ' . (!$this->is_superadmin ? ' AND forum_id IN(' . $this->allowed_forums . ') ' : '');
		$this->db->setQuery($sql);
		$topics = implode(', ', $this->db->loadResultArray());
		$topics = $topics ? $topics : 0;
		
		$this->db->setQuery('DELETE FROM #__agora_topics WHERE id IN (' . $topics . ')');
		$this->db->query();
        $this->db->setQuery('DELETE FROM #__agora_posts WHERE topic_id IN (' . $topics . ')');
        $this->db->query();
        $this->db->setQuery('DELETE FROM #__agora_subscriptions WHERE topic_id IN (' . $topics . ')');
        $this->db->query();
	}
	
	function getPostContent($post_id) {
		$this->db->setQuery('SELECT message FROM #__agora_posts WHERE id = ' . $this->db->Quote($post_id));
		$result = $this->db->loadResult();
		return $result;
	}
	
	function checkUnapproved($forum_id) {
		$sql = 'SELECT COUNT(*) ' .
				'FROM #__agora_posts AS p ' .
				'JOIN #__agora_topics AS t ' .
					'ON p.topic_id = t.id ' .
				'WHERE p.approved = 0 AND t.forum_id = ' . (int)$forum_id .
				' ORDER BY p.posted DESC';

		$this->db->setQuery($sql);
		return (int)$this->db->loadResult();
		
	}
	
	function getTopics() {
		$sql = 'SELECT t.*,p.id as post_id, p.poster_id, p.message FROM #__agora_posts p JOIN #__agora_topics t ON p.topic_id = t.id ' .
				'WHERE p.approved = 0 and t.approved = 0 ' . (!$this->is_superadmin ? ' AND t.forum_id IN(' . $this->allowed_forums . ') ' : '');
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}

	function getPosts() {
		$sql = 'SELECT p.*, t.forum_id, t.subject FROM #__agora_posts p JOIN #__agora_topics t ON p.topic_id = t.id ' .
				'WHERE p.approved = 0 and t.approved = 1 ' . (!$this->is_superadmin ? ' AND t.forum_id IN(' . $this->allowed_forums . ') ' : '');
		$this->db->setQuery($sql);
		return $this->db->loadAssocList();
	}

}

?>
